package com.itheima.reggic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggic.common.BaseContext;
import com.itheima.reggic.common.R;
import com.itheima.reggic.pojo.ShoppingCart;
import com.itheima.reggic.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加到购物车:{}", shoppingCart);
        if (shoppingCart == null)
            return R.error("添加失败!");
        Long uid = BaseContext.getCurrentId();
        shoppingCart.setUserId(uid);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 添加的用户
        queryWrapper.eq(ShoppingCart::getUserId, uid);
        // 判断添加的是什么
        if (shoppingCart.getDishId() != null)
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        else if (shoppingCart.getSetmealId() != null)
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        else
            return R.error("添加失败");
        // 查询是否已经存在
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one == null)
            // 不存在说明是第一次添加
            shoppingCartService.save(shoppingCart);
        else {
            // 否则就是第二次添加
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }
        return R.success("添加成功");
    }

    /**
     * 查询购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查询购物车");

        List<ShoppingCart> list = shoppingCartService.myList();

        return R.success(list);
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("减少数量{}", shoppingCart);
        if (shoppingCart == null)
            return R.error("减少失败!");

        Long uid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, uid);
        // 判断减少的是什么
        if (shoppingCart.getDishId() != null)
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        else if (shoppingCart.getSetmealId() != null)
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        else
            return R.error("减少失败");
        // 查询是否已经存在
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one == null)
            // 不存在则说明没有该商品
            return R.error("减少失败");
        else {
            // 否则就是第二次减少
            one.setNumber(one.getNumber() - 1);
            // 如果数量是0那么就删除订单
            if (one.getNumber() == 0) {
                shoppingCartService.removeById(one.getId());
            } else {
                shoppingCartService.updateById(one);
            }
        }
        return R.success("减少成功");
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        Long uid = BaseContext.getCurrentId();
        shoppingCartService.clean();
        return R.success("清空成功!");
    }
}
